package com.atzexu.web.controller;


import com.atzexu.domain.Assignment;
import com.atzexu.domain.User;
import com.atzexu.domain.dto.FindNotWorkDTO;
import com.atzexu.domain.dto.ShowWorkDTO;
import com.atzexu.domain.vo.NotWorkStudentVo;
import com.atzexu.domain.vo.UserWorkVo;
import com.atzexu.utils.R;
import com.atzexu.service.AssignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/work")
public class AssignmentController {

    @Autowired
    private AssignmentService assignmentService;


    /**
     * 发布作业
     *
     * @param assignment
     * @return
     */
    @PostMapping("/publishWork")
    public R publishWork(@RequestBody Assignment assignment, HttpSession httpSession) {
        Object user = httpSession.getAttribute("user");
        if (user instanceof User) {
            Integer id = ((User) user).getId();
            assignment.setUserId(id);
        }
        boolean flag = assignmentService.publishWork(assignment);
        if (flag) {
            return new R(0, "操作成功", "作业发布成功");
        } else {
            return new R(0, "操作失败", "作业发布失败");
        }
    }

    /**
     * 展示作业完成情况
     * @param showWorkDTO
     * @return
     */
    @PostMapping("/showWorks")
    public R showWorks(@RequestBody ShowWorkDTO showWorkDTO) {
        List<UserWorkVo> list = assignmentService.showWorks(showWorkDTO);
        return new R(0, "操作成功", list);
    }

    /**
     * 查询时间段未提交作业的学生以及次数
     * @param findNotWorkDTO
     * @return
     */
    @PostMapping("/findNotWorkByTime")
    public R findNotWorkByTime(@RequestBody FindNotWorkDTO findNotWorkDTO){
        List<NotWorkStudentVo> notWorkList = assignmentService.findNotWorkByTime(findNotWorkDTO);
        return new R(0,"操作成功",notWorkList);
    }
}
